package com.klhappy.houyuan.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.klhappy.houyuan.constant.KeyConstant;
import com.klhappy.houyuan.view.AbsActivity;
import com.klhappy.houyuan.R;

import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;

/**
 * Created by MrPu on 15/11/21.
 */
public class ExtractNextMoneyActivity extends AbsActivity {
    @InjectView(R.id.title)
    TextView title;
    @InjectView(R.id.et_pay)
    EditText et_pay;
    @InjectView(R.id.et_name)
    EditText et_name;
    @InjectView(R.id.et_all_money)
    EditText et_all_money;

    @InjectView(R.id.iv_one)
    ImageView iv_one;
    @InjectView(R.id.iv_two)
    ImageView iv_two;
    @InjectView(R.id.iv_three)
    ImageView iv_three;

    @OnClick(R.id.click_ra)
    public void click_ra(){
        finish();
    }
    @OnClick(R.id.iv_one)
    public void iv_one(){
        et_pay.setText("");
    }
    @OnClick(R.id.iv_two)
    public void iv_two(){
        et_name.setText("");
    }
    @OnClick(R.id.iv_three)
    public void iv_three(){
        et_all_money.setText("");
    }

    @OnClick(R.id.next_money)
    public void next_money(){
        getView();
    }

    private String money;
    private String accname;
    private String Account;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.extract_next_money);
        ButterKnife.inject(this);
        title.setText("提现申请");
        intView();
    }

    private void intView(){
        money = (String) getIntent().getSerializableExtra("money");
        Account = mConfigManage.get(KeyConstant.Account, "");
        accname = mConfigManage.get(KeyConstant.accname, "");

        if (!TextUtils.isEmpty(accname) && !TextUtils.isEmpty(Account)) {
            et_pay.setText(Account);
            et_name.setText(accname);
        }
        et_all_money.setHint("您的提现金额不超过" + money + "元");

        if (!et_pay.getText().toString().equals("")){
            iv_one.setVisibility(View.VISIBLE);
        }
        if (!et_name.getText().toString().equals("")){
            iv_two.setVisibility(View.VISIBLE);
        }
        if (!et_all_money.getText().toString().equals("")){
            iv_three.setVisibility(View.VISIBLE);
        }

        et_pay.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (!s.toString().equals("")) {
                    iv_one.setVisibility(View.VISIBLE);
                }else {
                    iv_one.setVisibility(View.GONE);
                }
            }
        });
        et_name.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (!s.toString().equals("")) {
                    iv_two.setVisibility(View.VISIBLE);
                }else {
                    iv_two.setVisibility(View.GONE);
                }
            }
        });
        et_all_money.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (!s.toString().equals("")) {
                    iv_three.setVisibility(View.VISIBLE);
                }else {
                    iv_three.setVisibility(View.GONE);
                }
            }
        });
    }

    private void getView(){
        String accnamex = et_pay.getText().toString();
        String Accountx = et_name.getText().toString();
        String moneyx = et_all_money.getText().toString();

        if (!TextUtils.isEmpty(accnamex) && !TextUtils.isEmpty(Accountx) && !TextUtils.isEmpty(moneyx)) {
            if (Float.parseFloat(moneyx) < 10){
                showToastx("提现金额不能小于10元");
            }else if (Float.parseFloat(moneyx) > Float.parseFloat(money)){
                showToastx("不能超出可提现金额");
            }else {
                Intent intent = new Intent(this ,ConfirmMoneyActivity.class);
                intent.putExtra("moneyx" ,moneyx);
                startActivity(intent);
                finish();
            }
        }else {
            showToastx("请正确填写");
        }
    }
}
